$(document).ready(function(){
	
	$("#assessments").accordion(
	{ 
		event: 'click', 
		autoHeight: false,
		collapsible: true,
		active: false,
		alwaysOpen: false,
		animated: false,
		autoheight: false,
	});

	$("#assessments").bind( "accordionchange", function(event, ui) {
		//alert('You could say something everytime they switch assessment views');
	});

	$("#sortable").sortable();

	$("#plus").click(function(){

		saveInputToArray();
		var myAnswer = new Array();
		myAnswer["answer_text"] = '';
		myAnswer["answer_correct"] = false;
		myAnswers.push(myAnswer);
		printAnswerArray();

	});

	$("#minus").click(function(){

		saveInputToArray();
		var isDelete = $('input[name=minus]:checked').val();

		if (isDelete){

			if (!confirm('Are you sure you want to delete this answer?')){
				return false;
			}

			myAnswers.splice(isDelete,1);
			printAnswerArray();
			
		}else{
			alert('Nothing Selected');
			return false;
		}
	
	});

	$("#reset").click(function(){

		myAnswers = new Array();
		$("#output").html('');
		alert('Answers Cleared');

	});

	$("#select_all").click(function(){

		var isSelected = $('#select_all').val();

		if (isSelected){
			confirm('value true:'+isSelected);
		}else{
			confirm('value false:'+isSelected);
		}

	});

	$("#answertype").change(function(){
		 
		var answertypeSelected = $('#answertype').val();

		if (answertypeSelected == 3 || answertypeSelected == 4){
			
			myAnswers = new Array();
			var myAnswer1 = new Array();
			var myAnswer2 = new Array();

			if (answertypeSelected == 3){
				myAnswer1["answer_text"] = 'Yes';
				myAnswer2["answer_text"] = 'No';
			}

			if (answertypeSelected == 4){
				myAnswer1["answer_text"] = 'True';
				myAnswer2["answer_text"] = 'False';
			}

			myAnswer1["answer_correct"] = false;
			myAnswer1["answer_readonly"] = true;
			myAnswer2["answer_correct"] = false;
			myAnswer2["answer_readonly"] = true;

			myAnswers.push(myAnswer1);
			myAnswers.push(myAnswer2);
			printAnswerArray();

		}

	});


});

function printAnswerArray(){
	
	$("#output").html('');

	var numberOfAnswers = myAnswers.length;
	
	if (numberOfAnswers == 0){
		return false;
	}
	
	var count = 1;

	var htmlString = '<table><tr><th>Delete</th><th>Answer</th><th>Correct</th></tr>';

	for (var i = 0; i < numberOfAnswers; i++) {

		var isCorrect = myAnswers[i]["answer_correct"];
		
		var checkBox = '';
		if (isCorrect == true){ checkBox = 'checked';
		}else{ checkBox = '';}

		var isReadOnly = myAnswers[i]["answer_readonly"];
		var readOnly = '';
		if (isReadOnly == true){ readOnly = 'readonly';
		}else{ readOnly = '';}

		htmlString += '<tr>';
		htmlString += '<td>'+count+'.'+'<input type="hidden" name="data[Answer_ID]['+i+']" id="answer_id_'+i+'" value="'+myAnswers[i]["answer_id"]+'"><input type="radio" name="minus" value="'+i+'" id="minus" align="center"></td>';
		htmlString += '<td><input align="center" '+readOnly+' type="text" size="30" id="answer_text_'+i+'" name="data[Answer_Text]['+i+']" value="'+myAnswers[i]["answer_text"]+'"></td>';
		htmlString += '<td><input align="center" type="checkbox" '+checkBox+' id="answer_correct_'+i+'" name="data[Answer_Correct]['+i+']" value="'+i+'"></td>';
		htmlString += '</tr>';
		count ++;

	}

	htmlString += '</table>';
	//alert(htmlString);
	$("#output").html(htmlString);

}

function saveInputToArray(){
	
	var numberOfAnswers = myAnswers.length;

	if (numberOfAnswers == 0){
		return false;
	}

	var myNewAnswers = new Array();

	for (var i = 0; i < numberOfAnswers; i++) {

		var myAnswer = new Array();

		myAnswer["answer_text"] = $('#answer_text_'+i).val();
		
		if ($('#answer_correct_'+i+':checked').val() == undefined) {
			myAnswer["answer_correct"] = false;
		}else{
			myAnswer["answer_correct"] = true;
		}

		myAnswer["answer_id"] = $('#answer_id_'+i).val();

		myNewAnswers.push(myAnswer);
	}

	myAnswers = myNewAnswers;

}

	
